<template>
    <!--微信组件-->
  <div id="wechat">
    <ul class="wechat-list">
        <!--props传递消息对象 baseMsgObj -->
        <msg-item v-for="baseMsgObj in messages " :item="baseMsgObj" class="list-row line-bottom" :key="baseMsgObj.mid"></msg-item>
    </ul>
  </div>
</template>
<script>

import search from "../common/search";
import msgItem from "../wechat/msg-item";

export default {
    components: {
        search,
        msgItem,
    },
    mixins: [window.mixin],
    data() {
        return {
            "pageName": "消息",
            messages:[],
            timer:null,
            myuserinfo:null,
        }
    },
    activated() {
      $('#return[tag=div]').remove();
      this.changeStyle();
      this.displayFoot();
      this.userStatus();
      this.queryInfo();
    },
    mounted() {

      this.changeStyle();
      this.displayFoot();
      this.userStatus();
      this.queryInfo();
    },
    methods: {
        changeStyle(name){
          try {
            var name = window.location.hash.slice(2);
            name = name.includes('?') ? name.split('?')[0] : name;
            name = name.includes('/') ? name.split('/')[0] : name;
            $(`#wx-nav dl`).not(`#wx-nav-${name}`).removeClass('router-link-exact-active');
            $(`#wx-nav dl`).not(`#wx-nav-${name}`).removeClass('router-link-active');
            $(`#wx-nav-${name}`).addClass('router-link-exact-active');
            $(`#wx-nav-${name}`).addClass('router-link-active');
            console.log(name);
          } catch (error) {
            console.log(error);
          }
        },
        displayFoot(){
          $('.app-footer').css('display','block');
        },
        async clearLoginInfo(){
          try {
            let info = await Betools.storage.getStore('system_linfo');

            this.username = info.username;
            this.password = info.password;

            Betools.storage.clearStore('system_userinfo');
            Betools.storage.clearStore('system_token');
            Betools.storage.clearStore('system_department');
            Betools.storage.clearStore('system_login_time');
          } catch (error) {
            console.log(error);
          }
        },
        async queryInfo(){

          const item = this.$store.state.msgList.baseMsg;

          //获取用户信息
          this.myuserinfo = await Betools.storage.getStore('system_userinfo');

          await this.queryMessages();

        },
        async queryMessages(){

          //查询当前路径
          const path = this.$router.currentRoute.path;

          //如果当前路径在wechat，则查询最新消息
          if(path == '/wechat'){

            try {
              //获取此用户的消息消息
              this.messages = await Betools.query.queryVMessages(this.myuserinfo.userid , this.myuserinfo.username);

              //将此用户的消息数据转为特定格式的数据
              this.messages.sort((n1,n2) => {
                return n2.id - n1.id;
              });
            } catch (error) {
              console.log(error);
            }

            //如果定时器存在，则清空原定时器
            if(!!window.wechatTimer){
              clearTimeout(window.wechatTimer);
            }

            try {
              //验收加载数据
              window.wechatTimer = this.timeer = setTimeout(async ()=>{
                await this.queryMessages();
              }, 300);
            } catch (error) {
              console.log(error);
            }

          }

        },
        async userStatus(){
          try {
            let info = await Betools.storage.getStore('system_userinfo');
            if( Betools.tools.isNull(info) ){
              vant.Toast('尚未登录！');
              await this.clearLoginInfo();
              this.$router.push(`/login`);
            }
          } catch (error) {
            console.log(error);
          }
        }
    }
}
</script>
<style scoped>
@import "../../assets/css/wechat.css";
</style>
